package com.huike.image_zoom;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;

/**
 * 图片缩放 demo
 */
public class MainActivity extends AppCompatActivity {

    private ImageView mIvMain;
    private LinearLayout ll_main;


    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mIvMain = (ImageView) findViewById(R.id.iv_main);
        ll_main =  findViewById(R.id.ll_main);

        //监听图片触摸
        ll_main.setOnTouchListener(new View.OnTouchListener() {
            float currentDistance;//开始值
            float lastDistance = -1;//结束值
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                /**
                 * 常用
                 * ACTION_MOVE //触摸
                 * ACTION_UP//手指抬起
                 * ACTION_DOWN//手指按下
                 */

                if(motionEvent.getAction() == MotionEvent.ACTION_MOVE){//监听类型为触摸
                    if(motionEvent.getPointerCount() >=2){//getPointerCount 得到手指在屏幕数量

                        float offsetX = motionEvent.getX(0) - motionEvent.getX(1);//得到X值 第一个手指 和第二个手指X坐标
                        float offsetY = motionEvent.getY(0) - motionEvent.getY(1);//得到Y值 第一个手指 和第二个手指Y坐标

                        currentDistance = (float) Math.sqrt(offsetX*offsetX + offsetY*offsetY);//初始化
                        Log.e("---",currentDistance +"");
                        /**
                         * Math.sqrt() 函数返回一个数的平方根，即：
                         * Math.sqrt(9); // 3
                         * Math.sqrt(2); // 1.414213562373095
                         */

                        if(currentDistance < 0){
                            lastDistance = currentDistance;
                        }else{
                                if(currentDistance - lastDistance > 5){//变大
                                    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mIvMain.getLayoutParams();
                                    params.width = (int) (1.1f * mIvMain.getWidth());
                                    params.height = (int) (1.1f * mIvMain.getHeight());
                                    mIvMain.setLayoutParams(params);
                                    lastDistance = currentDistance;//初始化
                                }else if(lastDistance - currentDistance > 5){//变大
                                    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mIvMain.getLayoutParams();
                                    params.width = (int) (0.9f * mIvMain.getWidth());
                                    params.height = (int) (0.9f * mIvMain.getHeight());
                                    mIvMain.setLayoutParams(params);
                                    lastDistance = currentDistance;
                                }
                        }
                    }
                }

                return true;
            }
        });
    }



}
